package com.miyu.client1.controller.user;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.miyu.client1.pojo.UserTableEntity;
import com.miyu.client1.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/  ")
    public  String registe(){

        String str = "index";
        return  str;
    }

    @Autowired
    private userService us;

    @RequestMapping("/save")
    @ResponseBody
    public String saveUser(@RequestBody String param){
        UserTableEntity ut = new UserTableEntity();
//        System.out.println(">>>>>>>>>>>>>>>>>>>"+param);
//        JSONObject jo = JSON.parseObject(param);
//        System.out.println(jo);
        us.save(ut);
    String str = "保存成功";
    return str;
    }

    @RequestMapping( "/queryData")
    @ResponseBody
    public List<UserTableEntity> queryData(@RequestBody String name){
        System.out.println(name);
        List<UserTableEntity> rs =  us.queryData(name);

        return  rs;
    }

    @ResponseBody
    @RequestMapping("/queryDataBySql")
    public List<Object>queryDataBySql(String name){
        System.out.println("name>>>>"+name);
        List<Object> rs = us.queryDataBySql();
        return  rs;
    }

    @RequestMapping("/queryDataBean")
    public List<UserTableEntity> queryData(@RequestBody UserTableEntity ute){

        System.out.println("实体数据>>>>>"+ute.getUserName());
        return null;
    }
    @RequestMapping("/queryDataAll")
    @ResponseBody
    public JSONObject queryData2(){
        List<UserTableEntity> rs =  us.queryData("");

        JSONArray ja = JSON.parseArray(JSON.toJSONString(rs));
        System.out.println("测试数据"+ja);
       // code":0,"msg":"","count":1000,"data
         JSONObject ps = new JSONObject();
         ps.put("code","0");
         ps.put("msg","chauxn");
         ps.put("data",ja);
        //JSONSerializer.toJSON(rs).toString();
        return  ps;
    }

    @RequestMapping("/logintest")
    @ResponseBody
    public String loginAlert(@RequestBody String str){
        System.out.println(str);
        return "test";
    }
}
